package cn.tedu.net.collection.generic;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

//本类用于测试collection接口
public class Test3_Collection {
    public static void main(String[] args) {
        //创建collection相关的接口对象
        Collection<Integer> c=new ArrayList<>();
    c.add(100);
    c.add(200);
    c.add(300);
    c.add(400);
    c.add(500);
        System.out.println(c);
       /* c.clear();
        System.out.println(c);*/
        c.hashCode();//返回此集合对象对应的哈希码值
        System.out.println(c.isEmpty());
        System.out.println(c.size());
        System.out.println(c.equals(200));
        System.out.println(c.contains(200));
        System.out.println(c.remove(100));
        System.out.println(c);
        Object[] a = c.toArray();//把集合中的元素放入数组
        System.out.println(Arrays.toString(a));
        Collection<Integer> c2=new ArrayList<>();
        c2.add(1);
        c2.add(2);
        c2.add(3);
        c2.add(300);
        System.out.println(c2);
        c.addAll(c2);
        System.out.println(c);
        System.out.println(c.contains(c2));// c集合中没有c2元素
        System.out.println(c.containsAll(c2));//c集合中有c2集合的所有元素
        System.out.println(c);
        System.out.println(c.retainAll(c2));//取两个集合的公共元素,直接修改集合,修改成功为ture
        System.out.println(c);
        System.out.println(c2);
        System.out.println(c.removeAll(c2));//删除c集合中属于c2的数据
        System.out.println(c);

        //遍历迭代集合中的元素Iterator<E> iterator
//        获取迭代器:c.iterator();
        //判断集合中是否有下个元素 it.hasNext();
        //获取当前迭代到的元素 it.next()
        c.iterator();
        Iterator<Integer> it=c.iterator();
        //通过集合迭代器,循环获取集合中的每个元素
        while(it.hasNext()){
            //hasNext()判断集合当中是否有下个元素,有就返回true,继续循环
            Integer num=it.next();
            System.out.println(num);
        }
    }
}
